feat: add dynamic filtering and pagination for Department search API #34
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request introduces dynamic filtering, pagination, and sorting for the Department search functionality, enhancing the /api/v1/departments/search endpoint with more flexible and efficient querying capabilities.
🔍 What’s New
Dynamic Filtering: Implemented using Spring Data JPA Specifications, allowing queries based on multiple optional criteria (departmentCode, departmentName, departmentDescription, etc.).
Pagination & Sorting: Integrated pageable and sort functionality to handle large datasets and improve response management.
Code Enhancements:
Added DepartmentSpecification.java for reusable query construction.
Updated DepartmentRepository.java to extend JpaSpecificationExecutor.
Modified DepartmentServiceImpl.java to apply specifications with pagination and sorting.
🧩 Technical Details
Uses CriteriaBuilder and Predicate to build dynamic queries at runtime.
Returns results as Page to maintain consistent response structure.
Backward compatible — no impact on existing Department CRUD operations.
Optimized for maintainability and future extensibility (e.g., filtering by date ranges or relationships).
🧪 How to Test
Run the Spring Boot application.
Open Swagger UI → http://localhost:8080/department-api/swagger-ui/index.html
Navigate to the POST /api/v1/departments/search endpoint.
Try the following example:
{
"departmentCode": "HR",
"departmentName": "eng",
"departmentDescription": "engineer",
"page": 0,
"size": 5,
"sortList": [
{ "field": "departmentName", "direction": "ASC" }
]
}
Verify results are filtered, paginated, and sorted dynamically.
🚀 Benefits
Enables multi-field search without hardcoding queries.
Improves performance and scalability for large datasets.
Makes the API more developer-friendly and extensible.
Follows Spring Boot 3 + Java 21 best practices.
✅ Summary
This PR makes the Department API more powerful and production-ready by introducing a dynamic search system with pagination and sorting, paving the way for advanced enterprise-grade querying capabilities.